#include "ProjectHeader.h"
#include "UIElement-Slider.h"

#pragma region SetterMethods

void UISliderElement::SetSliderBackground(texCoord2f sliderBackgroundTexture)
{
	_sliderBackgroundTexture = sliderBackgroundTexture;
	UIElement::_bgDirty = true;
}

void UISliderElement::SetSliderTexture(texCoord2f sliderTexture)
{
	_sliderTexture = sliderTexture;
	UIElement::_fgDirty = true;
}

void UISliderElement::SetSliderMode(sliderTypeEnum sliderType, int min , int max)
{
	_sliderType = sliderType;
	if(min < 0){min = 0;}
	if(max < 0){max = 0;}
	if(min < max)
	{
		_sliderMin = min;
		_sliderMax = max;
	}
	else if(min > max)
	{
		_sliderMin = max;
		_sliderMax = min;
	}
	if(min == max){min = 0; max = 1;}	//HACK this sets min,max to 0,1 if they are equal otherwise.
}

void UISliderElement::SetSliderPosition(vertex2f btmLft)
{
	SetSliderAreaRectangle(btmLft);
	UIElement::_fgDirty = true;
}

void UISliderElement::SetSliderSize(vertex2f size)
{
	_size = size;
	SetSliderAreaRectangle(_sliderArea.btmLft);
	UIElement::_fgDirty = true;
}

void UISliderElement::SetSliderAreaRectangle(vertex2f position)
{
	_sliderArea = UIElement::CalculateRectangle(position, _sliderSize);
}
#pragma endregion

#pragma region DrawMethods
bool UISliderElement::Draw(std::vector<vertex> *VBOVec, const std::map<int,texCoord2f> &texMap, GLuint currentTex)
{
	if(VBOVec == NULL)
	{
		return DrawBackground(VBOVec, texMap, currentTex) || DrawForeground(VBOVec, texMap, currentTex);
	}
	return DrawBackground(VBOVec, texMap, currentTex) && DrawForeground(VBOVec, texMap, currentTex);
}
bool UISliderElement::DrawBackground(std::vector<vertex> *VBOVec, const std::map<int,texCoord2f> &texMap, GLuint currentTex)
{
	if(VBOVec == NULL)
	{
		return (UIElement::DrawBackground(VBOVec, texMap, currentTex) ||
			DrawSliderSpecificBackgrounds(VBOVec, texMap, currentTex)	);
	}
	UIElement::_bgDirty = false;
	return( UIElement::DrawBackground(VBOVec, texMap, currentTex) ||
	 DrawSliderSpecificBackgrounds(VBOVec, texMap, currentTex));

}

bool UISliderElement::DrawForeground(std::vector<vertex> *VBOVec, const std::map<int,texCoord2f> &texMap, GLuint currentTex)
{
	if(VBOVec == NULL)
	{
		return UIElement::StatValueHasChanged() || UIElement::_fgDirty;
	}
	if(currentTex == _sliderBackgroundTexture.TexIndex)
	{
		UIElement::_fgDirty = false;
		float sliderValue = 1.0f;
		if(_sliderMax != _sliderMin){(UIElement::_lastStatValue - _sliderMin) / (_sliderMax - _sliderMin);}
		//Prevents dividing by 0

		rectangle tempRectangle = CalculateSliderBarAreaRectangle(sliderValue);

		texCoord2f SliderBarTexture = CalculateSliderBarTexture(sliderValue);

		UIElement::DrawRectangle(VBOVec, tempRectangle, SliderBarTexture, 1.0f);	//HACK depth
		return true;
	}
	return false;
}

bool UISliderElement::DrawSliderSpecificBackgrounds(std::vector<vertex> * VBOVec, const std::map<int, texCoord2f>& texMap, GLuint currentTex)
{
	//HACK
	if(VBOVec == NULL){return UIElement::_bgDirty;}
	if(currentTex == _sliderBackgroundTexture.TexIndex)
	{
		UIElement::DrawRectangle(VBOVec, _sliderArea, _sliderBackgroundTexture, 2.0f);	//HACK depth
		return true;
	}
	return false;	//Return false if we don't add any verts
}
#pragma endregion

rectangle UISliderElement::CalculateSliderBarAreaRectangle(float size)
{
	vertex2f sliderBarSize;
	sliderBarSize.u = size * _sliderSize.u;
	sliderBarSize.v = _sliderSize.v;
	return UIElement::CalculateRectangle(_sliderSize, sliderBarSize);
}


texCoord2f UISliderElement::CalculateSliderBarTexture(float size)
{
	texCoord2f SliderBarTexture;
	if( _sliderType == clipFront)
	{
		SliderBarTexture = _sliderTexture;
		SliderBarTexture.btmRgt.u = SliderBarTexture.btmLft.u + ((SliderBarTexture.btmRgt.u - SliderBarTexture.btmLft.u) * size);
		SliderBarTexture.topRgt.u = SliderBarTexture.topLft.u + ((SliderBarTexture.topRgt.u - SliderBarTexture.topLft.u) * size);
		return SliderBarTexture;
	}
	else if ( _sliderType == clipBack)
	{
		SliderBarTexture = _sliderTexture;
		SliderBarTexture.btmLft.u = SliderBarTexture.btmRgt.u - ((SliderBarTexture.btmRgt.u - SliderBarTexture.btmLft.u) * size);
		SliderBarTexture.topLft.u = SliderBarTexture.topRgt.u - ((SliderBarTexture.topRgt.u - SliderBarTexture.topLft.u) * size);
		return SliderBarTexture;
	}
	else if ( _sliderType == stretch)
	{
		return _sliderTexture;
	}
}

bool UISliderElement::StatValueHasChanged()
{
	bool statHasChanged = UISliderElement::StatValueHasChanged();
	if		(UIElement::_lastStatValue < _sliderMin){UIElement::_lastStatValue = _sliderMin;}
	else if	(UIElement::_lastStatValue > _sliderMax){UIElement::_lastStatValue = _sliderMax;}
	return statHasChanged;
}